package leetcode;

import java.util.Scanner;
import java.util.Stack;

public class MyStack {
    //括号匹配
    public boolean isValid(String S) {
        char[] s=S.toCharArray();
        Stack<Character> stack=new Stack<>();
        for (char c:s){
            if(c=='{'||c=='('||c=='['){
                stack.push(c);
            }else {
                if(stack.isEmpty()){
                    return false;
                }
                if(c=='}'&&stack.peek()=='{'||c==')'&&stack.peek()=='('||c==']'&&stack.peek()=='['){
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        if(stack.isEmpty()){
            return true;
        }else return false;
    }
    //逆波兰表达式
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack=new Stack<>();
        for (String s:tokens){
            if (!isOperation(s)) {
                stack.push(Integer.parseInt(s));
            }else {
                int right=stack.pop();
                int left=stack.pop();
                switch (s){
                    case "+":
                        stack.push(left+right);
                        break;
                    case "-":
                        stack.push(left-right);
                        break;
                    case "*":
                        stack.push(left*right);
                        break;
                    case "/":
                        stack.push(left/right);
                        break;
                }
            }
        }
        return stack.peek();
    }
    public boolean isOperation(String s){
        if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")){
            return true;
        }else return false;
    }
    //栈的压入弹出序列
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack=new Stack<>();
        for (int i=0;i<pushV.length;i++){
            int j=0;
            stack.push(pushV[i]);
            while (!stack.isEmpty()&&j<popV.length&&popV[j]==stack.peek()){
                stack.pop();
                j++;
            }
        }
        if(stack.isEmpty()){
            return true;
        }else return false;
    }
}
